package com.learn.jpatest.controller;

import cn.hutool.core.lang.Dict;
import com.learn.jpatest.model.BaseNote;
import com.learn.jpatest.model.Note;
import com.learn.jpatest.repository.BaseNoteRepository;
import com.learn.jpatest.repository.NoteRepository;
import com.learn.jpatest.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class NoteController {

    @Autowired
    NoteService noteService;

    // Get All Notes
//    @GetMapping("/notes")
//    public List<? extends BaseNote> getAllNotes() {
//        return noteRepository.findAll();
//    }

    // Create a new Note
    @PostMapping("/notes/{tableName}")
    public BaseNote createNote(@PathVariable(value = "tableName") String tableName, @RequestBody Map noteMap) {
        return noteService.createNote(noteMap, tableName);
    }

    // Get a Single Note
    @GetMapping("/notes/{tableName}/{id}")
    public Dict getNoteById(@PathVariable(value = "id") Long noteId, @PathVariable(value = "tableName") String tableName) {
        return noteService.getNoteById(noteId, tableName);
    }

//    // Update a Note
//    @PutMapping("/notes/{id}")
//    public BaseNote updateNote(@PathVariable(value = "id") Long noteId,
//                           @Valid @RequestBody BaseNote noteDetails) {
//
//        BaseNote note = noteRepository.findById(noteId).get();
//        note.setTitle(noteDetails.getTitle());
//        note.setContent(noteDetails.getContent());
//        BaseNote updatedNote = noteRepository.save(note);
//        return updatedNote;
//    }
//
//    // Delete a Note
//    @DeleteMapping("/notes/{id}")
//    public ResponseEntity<?> deleteNote(@PathVariable(value = "id") Long noteId) {
//        BaseNote note = noteRepository.findById(noteId).get();
//        noteRepository.delete(note);
//        return ResponseEntity.ok().build();
//    }

}
